package com.demo.petshop.controller;

import com.demo.petshop.entity.Products;
import com.demo.petshop.service.ProductsService;
import com.demo.petshop.util.Result;
import com.mybatisflex.core.paginate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

/**
 *  控制层。
 *
 * @author lenovo
 * @since 2025-06-11
 */
@RestController
@RequestMapping("/products")
public class ProductsController {

    @Autowired
    private ProductsService productsService;
    @PostMapping("save")
    public boolean save(@RequestBody Products products) {
        return productsService.save(products);
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    public boolean remove(@PathVariable Serializable id) {
        return productsService.removeById(id);
    }


    @PutMapping("update")
    public boolean update(@RequestBody Products products) {
        return productsService.updateById(products);
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("productList")
    public Result productList(){
        return Result.success(productsService.productList());
    }

    @GetMapping("search")
    public Object search(@RequestParam String keyword){
        return productsService.search(keyword);
    }

    @GetMapping("category")
    public Object category(@RequestParam int categoryId){
        return productsService.category(categoryId);
    }

    @GetMapping("product")
    public Object product(@RequestParam int productId){
        return productsService.product(productId);
    }

    @GetMapping("orderBySales")
    public Object orderBySale(int categoryId){
        return productsService.orderBySale(categoryId);
    }

    @GetMapping("orderByPrice")
    public Object orderByPrice(int categoryId){
        return productsService.orderByPrice(categoryId);
    }

    @GetMapping("reOrderByPrice")
    public Object reOrderByPrice(int categoryId){
        return productsService.reOrderByPrice(categoryId);
    }

    @GetMapping("specs")
    public Object getProductSpec(int productId){
        return productsService.getProductSpec(productId);
    }

    @GetMapping("getProductVideo")
    public Object getProductVideos(int productId){
        return productsService.getProductVideos(productId);
    }

    @GetMapping("getStoreByProductId")
    public Object getStoreByProductId(int productId){
        return productsService.getStoreByProductId(productId);
    }
    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    public Products getInfo(@PathVariable Serializable id) {
        return productsService.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<Products> page(Page<Products> page) {
        return productsService.page(page);
    }

    

    @GetMapping("video")
    public Object getByVideoId(@RequestParam Integer videoId){
        return productsService.getByVideoId(videoId);
    }
    
    /**
     * 获取商品评价列表
     *
     * @param productId 商品ID
     * @return 评价列表
     */
    @GetMapping("/{productId}/reviews")
    public Object getProductReviews(
            @PathVariable Integer productId) {
        return Result.success(productsService.getProductReviews(productId));
    }
    @PostMapping("/update")
    public Object updateProduct(@RequestBody Products products){
        return productsService.updateProduct(products);
    }

    @GetMapping("getProductsListByMemberLevel")
    public Result getProductsListByMemberLevel(){
        return Result.success(productsService.getProductsListByMemberLevel());
    }
    @PostMapping("create")
    public Result createProduct(@RequestBody Products products){
        return Result.success(productsService.createProduct(products));
    }

    @DeleteMapping("delete")
    public Result delete(@RequestParam Integer productId){
        return Result.success(productsService.delete(productId));
    }

}
